package compsup;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import offer.Offer;
import placementDiary.PlacementDiary;
import query.Query;

import recruiter.Recruiter;
import student.Student;

import user.User;
import user.UserHome;

import company.Company;
import cv.CV;

import degree.Degree;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;


/**
 * CompanySupBean
 * 
 * 
 * The CompanySupBean class implements all the methods that a remote client expect to find
 * when it calls the interface (CompanySup).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class CompanySupBean implements EntityBean{
	
	InitialContext initialContext;
	UserHome userHome;

	// <ATTRIBUTES>
	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public String getLogin(){
		return getUser().getLogin();
	}
	public void setLogin(String login){
		getUser().setLogin(login);
	}
	
	public String getPassword(){
		return getUser().getPasswd();
	}
	public void setPassword(String password){
		getUser().setPasswd(password);
	}
	
	public String getFirstname(){
		return getUser().getFirstName();
	}
	public void setFirstname(String firstname){
		getUser().setFirstName(firstname);
	}
	
	public String getSurname(){
		return getUser().getName();
	}
	public void setSurname(String surname){
		getUser().setName(surname);
	}
	
	public String getEmail(){
		return getUser().getMail();
	}
	public void setEmail(String email){
		getUser().setMail(email);
	}
	
	// Bean attributes
	// relations 1-1
	public abstract User getUser();
	public abstract void setUser(User user);
	
	public abstract Company getCompany();
	public abstract void setCompany(Company company);
	
	
	// relation 1-many
	public abstract Collection getStudents();
	public abstract void setStudents(Collection students);
	
	public abstract Collection getDegrees();
	public abstract void setDegrees(Collection degrees);
	// </ATTRIBUTES>
	
	// <BEANS METHODS>
	
	public String ejbCreate(String firstName,String name, String login,String passwd,String email, Company company) throws CreateException{
		try{
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("MyUser_L");
		}catch (NamingException e) {
			e.printStackTrace();
		}
		User u = null;
		u = userHome.create(firstName, name, login, passwd, email);
		u.setType(User.COMPSUP);
		setId(u.getId());
		return null;
	}

	public void ejbPostCreate(String firstName,String name, String login,String passwd,String email, Company company){
		{
			UserHome userHome = null;
			CompanySupHome compsupHome = null;
			try{
				initialContext = new InitialContext();
				userHome = (UserHome)initialContext.lookup("MyUser_L");
				compsupHome = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
			}catch (NamingException e) {
				e.printStackTrace();
			}
			User u = null;
			CompanySup cs = null;
			try 
			{
				u = userHome.findByPrimaryKey(getId());
				cs = compsupHome.findByPrimaryKey(getId());
			} 
			catch (FinderException e) 
			{
				e.printStackTrace();
			}
			setUser(u);
			setCompany(company);
			setStudents(new Vector());
			setDegrees(new Vector());
		}
	}
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws EJBException, RemoteException, RemoveException {
		int id = getId();
		User u = null;
		UserHome userHome = null;
		try {
			InitialContext initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("MyUser_L");
		} catch (NamingException e1) {
			e1.printStackTrace();
		}
		try {
			u = userHome.findByPrimaryKey(id);
		} catch (FinderException e) {
			e.printStackTrace();
		}
		try {
			u.remove();
		} catch (RemoveException e) {
			e.printStackTrace();
		}
	}
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	public void unsetEntityContext() throws EJBException, RemoteException {}
	
	// </ BEANS METHODS>
	// < METHODS>
	public CompanySupDescriptor toCompanySupDescriptor(){
		int [] studentIds = new int[this.getStudents().size()];
		int i = 0;
		for (Object o:getStudents()){
			studentIds[i] = ((Student)o).getId();
			i++;
		}
		int [] degreeIds = new int[this.getDegrees().size()];
		int j = 0;
		for (Object o:getDegrees()){
			degreeIds[j] = ((Degree)o).getId();
			j++;
		}
		return new CompanySupDescriptor(getId(),getFirstname(),getSurname(),getLogin(),getPassword(),getEmail(),getCompany().getId(),degreeIds,studentIds);
	
	}
	
	public CompanySupDescriptor toSimpleCompanySupDescriptor()
	{
		return new CompanySupDescriptor(getId(),getFirstname(),getSurname(),getLogin(),getPassword(),getEmail(),getCompany().getId(),getCompany().getName());
	}
	// </ METHODS>
}
